﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace RMSM
{
    [Serializable()]
    public class DUPhieuXuatKBChiTietBOCollection : Csla.BusinessListBase<DUPhieuXuatKBChiTietBOCollection, DUPhieuXuatKBChiTietBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            DUPhieuXuatKBChiTietBO _DUPhieuXuatKBChiTietBO = DUPhieuXuatKBChiTietBO.NewChildDUPhieuXuatKBChiTietBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_DUPhieuXuatKBChiTietBO));
            Add(_DUPhieuXuatKBChiTietBO);
            _DUPhieuXuatKBChiTietBO.STT0 = this.Count;
            return _DUPhieuXuatKBChiTietBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private DUPhieuXuatKBChiTietBOCollection()
        {
        }

        public static DUPhieuXuatKBChiTietBOCollection NewDUPhieuXuatKBChiTietBOCollection()
        {
            return new DUPhieuXuatKBChiTietBOCollection();
        }

        public static DUPhieuXuatKBChiTietBO NewDUPhieuXuatKBChiTietBO()
        {
            return DUPhieuXuatKBChiTietBO.NewChildDUPhieuXuatKBChiTietBO();
        }

        public static DUPhieuXuatKBChiTietBOCollection GetDUPhieuXuatKBChiTietBOCollection()
        {
            SPParam spParam = new SPParam("DUPhieuXuatKBChiTietID", "ASC", "WHERE Deleted=0");
            return DataPortal.Fetch<DUPhieuXuatKBChiTietBOCollection>(spParam);
        }

        public static DUPhieuXuatKBChiTietBOCollection GetDUPhieuXuatKBChiTietBOCollection(string where)
        {
            SPParam spParam = new SPParam("DUPhieuXuatKBChiTietID", "ASC", "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DUPhieuXuatKBChiTietBOCollection>(spParam);
        }

        public static DUPhieuXuatKBChiTietBOCollection GetDUPhieuXuatKBChiTietBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DUPhieuXuatKBChiTietID" : orderByColumn), "ASC", "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DUPhieuXuatKBChiTietBOCollection>(spParam);
        }

        public static DUPhieuXuatKBChiTietBOCollection GetDUPhieuXuatKBChiTietBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DUPhieuXuatKBChiTietID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DUPhieuXuatKBChiTietBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuXuatKBChiTietBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int stt = 0;
                while (dr.Read())
                {
                    DUPhieuXuatKBChiTietBO objDUPhieuXuatKBChiTietBO = DUPhieuXuatKBChiTietBO.GetChildDUPhieuXuatKBChiTietBO(dr);
                    objDUPhieuXuatKBChiTietBO.STT0 = ++stt;
                    this.Add(objDUPhieuXuatKBChiTietBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            foreach (DUPhieuXuatKBChiTietBO child in this)
            {
                if (child.IsNew && !StaticUtil.GetBool(child.Deleted))
                    child.Insert();
                else
                    child.Update();
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public DUPhieuXuatKBChiTietBO GetDUPhieuXuatKBChiTietBO(int _DUPhieuXuatKBChiTietID)
        {
            DUPhieuXuatKBChiTietBO returnObject = null;
            foreach (DUPhieuXuatKBChiTietBO _DUPhieuXuatKBChiTietBO in this)
            {
                if (_DUPhieuXuatKBChiTietBO.DUPhieuXuatKBChiTietID == _DUPhieuXuatKBChiTietID)
                {
                    returnObject = _DUPhieuXuatKBChiTietBO;
                    break;
                }
            }
            return returnObject;
        }

        public void Remove(int DUPhieuXuatKBChiTietID)
        {
            foreach (DUPhieuXuatKBChiTietBO _DUPhieuXuatKBChiTietBO in this)
            {
                if (_DUPhieuXuatKBChiTietBO.DUPhieuXuatKBChiTietID == DUPhieuXuatKBChiTietID)
                {
                    try
                    {
                        this.Remove(_DUPhieuXuatKBChiTietBO);
                    }
                    catch { }
                    break;
                }
            }
        }


        public int CheckExits(int ThuocID, int DonViTinh)
        {
            int check = -1;
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if ((this[i].ThuocID == ThuocID) & (this[i].DonViTinhID == DonViTinh))
                {
                    try
                    {
                        check = i;
                    }
                    catch { }
                    break;
                }
            }
            return check;
        }

        public int CheckExits(int STT)
        {
            int check = -1;
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].STT0 == STT)
                {
                    try
                    {
                        check = i;
                    }
                    catch { }
                    break;
                }
            }
            return check;
        }

        #endregion Business Method
        //==========================================================================
    }
}
